home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 003 / datenote.tqt / DATENOTE.TXT
Encoding:
Text File  |  1985-05-10  |  5.3 KB  |  160 lines

  1.  
  2. Date Formatting With dBASE III
  3. ------------------------------
  4. by Chris White
  5.  
  6.  
  7. There are often occasions when you may wish to display date
  8. variables in formats other than those allowed by the SET DATE
  9. command in version 1.1.  (See the dBASE III version 1.1 manual
  10. insert, "CORRECTIONS TO THE MANUAL AND PRODUCT CHANGE SUMMARY"
  11. page 5 and 6, for the various date formats allowed.)
  12.  
  13. Specifically, you may wish to display the date in a format such
  14. as January 25, 1984.  This is useful for check printing, invoice
  15. writing, report formats conforming to standards, or merely for
  16. aesthetic reasons.
  17.  
  18. The most common method of converting a date variable to a
  19. formatted string consists of using the SUBSTR, STR, and various
  20. date functions in combination.  As an example, let us convert the
  21. system date by assigning it to a character string variable and
  22. then displaying the variable with the @...SAY command.  Assuming
  23. the system date is 01/25/84, the desired output will look like
  24. "January 25, 1984."  The statement that makes the conversion is
  25. as follows:
  26.  
  27.       datestrg = CMONTH(DATE())+" "+STR(DAY(DATE()),2)+", "+;
  28.       STR(YEAR(DATE()),4)
  29.  
  30. To understand this expression, let us examine it in some detail.
  31. The memory variable datestrg will contain the formatted date
  32. string.  The first portion of the expression,
  33.  
  34.       CMONTH(DATE())
  35.  
  36. returns the month name as a character string, "January," in our
  37. example.  A blank character is concatenated onto the end of the
  38. month name with +" ".  The second portion of the expression,
  39.  
  40.       STR(DAY(DATE()),2)
  41.  
  42. returns the numeric day of the month as a character string.  A
  43. comma and a blank are concatenated with +", ".  We have so far
  44. "January 25,".  The last portion of the expression,
  45.  
  46.       STR(YEAR(DATE()),4)
  47.  
  48. returns the year as a four-character string.  The results of this
  49. last expression are concatenated to the month and day.  The
  50. memory variable, datestrg, will at this point contain "January
  51. 25, 1984".  Finally, to display the string to the screen or
  52. printer execute the command:
  53.  
  54.       @ 10,10 SAY datestrg
  55.  
  56. It is not necessary to assign the result of the expression to a
  57. memory variable in order to display it.  The @...SAY could well
  58. look like:
  59.  
  60.       @ 10,10 SAY CMONTH(DATE())+" "+STR(DAY(DATE()),2)+", "+;
  61.                 STR(YEAR(DATE()),4)
  62.  
  63. Additionally, this date expression can be used as the field
  64. contents in either the REPORT or LABEL FORM or any other display
  65. command that accepts a character string expression.  For other
  66. formatting algorithms please refer to the example that follows.
  67.  
  68.  
  69. >>> General Date Formats
  70.  
  71. For those who have been building a general purpose library of
  72. useful routines, the following procedure may be a beneficial
  73. addition.
  74.  
  75. This procedure can be used as a stand-alone command file or as a
  76. procedure in a PROCEDURE file.  It uses the PARAMETERS clause to
  77. allow the routine to be general and useful for a variety of
  78. different programs without change.  For further discussion on the
  79. use of the PARAMETERS clause, please refer to the dBASE III
  80. manual page 4-76 for version 1.0 or page 4-83 for version 1.1.
  81.  
  82. The construction of the routine is quite simple.  It takes two
  83. inputs, a date variable to format and a numeric code designating
  84. the format desired, and produces one output, a character variable
  85. containing the formatted date string.  The syntax to call the
  86. routine is as follows:
  87.  
  88.  
  89. STORE " " TO finv_date
  90. ----------------------
  91.        |__________________________ Receiving variable
  92.                                    must exist prior to call
  93. DO fdate WITH inv_date,5,finv_date
  94.    ----- ---- -------  - ---------
  95.      |    |      |     |     |_____ Character variable to
  96.      |    |      |     |            receive formatted date
  97.      |    |      |     |
  98.      |    |      |     |___________ Format code
  99.      |    |      |
  100.      |    |       ----------------- Date variable to format
  101.      |    |
  102.      |     ------------------------ Specifies PARAMETER list
  103.      |
  104.       ----------------------------- Format routine name
  105.  
  106.  
  107. The formats that are supported are as follows:
  108.  
  109. 1. Mon DD, YYYY
  110.  
  111. 2. Mon YYYY
  112.  
  113. 3. Mon DD
  114.  
  115. 4. Month DD, YYYY
  116.  
  117. 5. DD-Mon-YYYY
  118.  
  119.  
  120. >>> Program for General Date Formats
  121.  
  122. * Program..: Fdate.PRG
  123. * Author...: Chris White
  124. * Date.....: January 25, 1985
  125. * Note(s)..: This program takes a date variable passed to it
  126. *            with the PARAMETERS phrase and returns the date
  127. *            as a formatted character string.
  128. *
  129. PARAMETERS date,code,datestrg
  130. DO CASE
  131.    CASE code = 1
  132.    * ---"Jan 25, 1984"
  133.         datestrg = SUBSTR(CMONTH(date),1,3) + ' ' +;
  134.                    STR(DAY(date),2) + ', ' + STR(YEAR(date),4)
  135.    CASE code = 2
  136.    * ---"Jan 1984"
  137.         datestrg = SUBSTR(CMONTH(date),1,3) + ' ' +;
  138.                    STR(YEAR(date),4)
  139.    CASE code = 3
  140.    * ---"Jan 25"
  141.         datestrg = SUBSTR(CMONTH(date),1,3) + ' ' +;
  142.                    STR(DAY(date),2)
  143.    CASE code = 4
  144.    * ---"January 25, 1984"
  145.         datestrg = CMONTH(date) + ' ' +;
  146.                    STR(DAY(date),2) + ', ' + STR(YEAR(date),4)
  147.    CASE code = 5
  148.    * ---"25-Jan-84"
  149.         datestrg = STR(DAY(date),2) + '-' +;
  150.                    SUBSTR(CMONTH(date),1,3) +;
  151.                    '-' + SUBSTR(STR(YEAR(date),4),3,2)
  152.    OTHERWISE
  153.         datestrg = 'Error'
  154. ENDCASE
  155. RETURN
  156. * EOF: Fdate.PRG
  157.  
  158.  
  159. --End--
  160.